package com.nageffer.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nageffer.shortlink.admin.common.convention.result.Result;
import com.nageffer.shortlink.admin.common.convention.result.Results;
import com.nageffer.shortlink.admin.dto.req.UserLoginReqDTO;
import com.nageffer.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.nageffer.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.nageffer.shortlink.admin.dto.resp.UserActualRespDTO;
import com.nageffer.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.nageffer.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名字获取用户
     * 先把这个方法关了，否则查询用户名是否已经存在老是进入这个方法
     */
    /*
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        *//*
        UserRespDTO result = userService.getUserByUsername(username);
        if (result == null){
            return new Result<UserRespDTO>().setCode(UserErrorCodeEnum.USER_NULL.code()).setMessage(UserErrorCodeEnum.USER_NULL.message());
        }else {
            return Results.success(result);
        }
        *//*
        return Results.success(userService.getUserByUsername(username));
    }
    */

    /**
     * 根据用户名字获取用户(无脱敏)
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username){
        //BeanUtil.toBean:对象转换成UserActualRespDTO类型
        UserActualRespDTO userActualRespDTO = BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class);
        return Results.success(userActualRespDTO);
    }

    /**
     * 查询用户名是否已经存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 用户注册
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO registerParam){
        userService.register(registerParam);
        return Results.success();
    }

    /**
     * 更新用户
     * 用户名不修改
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> updateUser(@RequestBody UserUpdateReqDTO userUpdateReqDTO){
        userService.updateUser(userUpdateReqDTO);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO){
        UserLoginRespDTO login = userService.login(userLoginReqDTO);
        return Results.success(login);
    }

    /**
     * 检查用户是否登陆
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        Boolean hasLogin = userService.checkLogin(username, token);
        return Results.success(hasLogin);
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username, token);
        return Results.success();
    }

}
